package com.github.houkunlin.extension;

import com.github.houkunlin.config.ConfigService;
import com.github.houkunlin.config.Settings;
import com.github.houkunlin.model.DataTableModel;
import com.github.houkunlin.ui.TemplateEditorNotificationProvider;
import com.github.houkunlin.util.PluginUtils;
import com.github.houkunlin.vo.Variable;
import com.github.houkunlin.vo.impl.RootModel;
import com.intellij.database.dialects.base.generator.ScriptGeneratorDiagnosticListener;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.weinisuo.plugin.generate.service.TemplateModelProvider;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author tanqi
 * @Description 添加数据源
 * @Date 2022/10/10 19:45
 * @Version 1.0
 */
public class DataSourceModelProvider implements TemplateModelProvider, ScriptGeneratorDiagnosticListener {

    @Override
    public Map<String, Object> getTemplateModel(AnActionEvent event) {
        Project project = event.getProject();
        assert project != null;
        PluginUtils.setProject(project);
        PluginUtils.syncResources();
        ConfigService configService = ConfigService.getInstance(project);
        if (configService == null) {
            Messages.showWarningDialog("初始化配置信息失败，但并不影响继续使用！", "Database Generator");
            configService = new ConfigService();
        }else {
            configService.refresh();
        }
        Settings settings = configService.getSettings();

        Map<String, Object> map = new HashMap<>();
        map.put("settings", settings);
        map.put("developer", configService.getDeveloper());
        map.put("gen", Variable.getInstance());
        map.put("date", DateTime.now());

        if(settings == null || StringUtils.isBlank(settings.getTableName())){
            return map;
        }
        DataSourceService dataSourceService = DataSourceService.getInstance();
        String tableName = settings.getTableName();
        RootModel rootModel = null;
        DataTableModel dataTableModel = TemplateEditorNotificationProvider
            .getProjectDataTableModelMap().get(project.getLocationHash());
        if(dataTableModel != null){
            rootModel = dataTableModel.getSelectedModel();
        }
        if (rootModel == null) {
            rootModel = dataSourceService.toModel(tableName);
        }
        if(MapUtils.isNotEmpty(settings.getGlobalVariables())){
            map.putAll(settings.getGlobalVariables());
        }
        if (rootModel == null) {
            return map;
        }
        map.put("table", rootModel.getTable());
        map.put("columns", rootModel.getColumns());
        map.put("entity", rootModel.getEntity(settings));
        map.put("fields", rootModel.getFields());
        map.put("primary", rootModel.getPrimary());
        return map;
    }
}
